﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Collections.Concurrent;
using System.Net;
using System.Text.RegularExpressions;

namespace work8
{
 //做一个窗体应用程序，接收用户输入的关键字，用户点击搜索时使用baidu搜索引擎搜索关键字，
 //从搜索结果中，摘抄文字部分的前200个字，分别显示到两个多行文本框内。请使用多线程方式、
 //并行编程或者异步编程方式完成任务。

    public partial class Form1 : Form
    {
        private delegate void MyDelegate();

        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Task task1 = new Task(() => { Baidu_Search1("https://baidu.com/baidu?wd=" + textBox1.Text); });
            Task task2 = new Task(() => { Baidu_Search2("https://baidu.com/baidu?wd=" + textBox1.Text); });
            task1.Start();
            task2.Start();
        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {

        }

        private void Baidu_Search1(string url)
        {
            WebClient webClient = new WebClient();
            byte[] data = webClient.DownloadData(url);
            string response = Encoding.UTF8.GetString(data);
            response = Regex.Replace(response, @"[^\u4e00-\u9fa5]", "");
            this.BeginInvoke(new MyDelegate(() => { textBox2.Text = response.Substring(0, 200); }));
        }
        //response = Regex.Replace(response, @"[^\u4e00-\u9fa5]", "");
         //   this.BeginInvoke(new MyDelegate(() => { textBox2.Text = response.Substring(0, 200); }));
        //this.BeginInvoke(new MyDelegate(() => { textBox2.Text = response; }));
        private void Baidu_Search2(string url)
        {
            WebClient webClient = new WebClient();
            byte[] data = webClient.DownloadData(url);
            string response = Encoding.UTF8.GetString(data);
            response = Regex.Replace(response, @"[^\u4e00-\u9fa5]", "");
            this.BeginInvoke(new MyDelegate(() => { textBox3.Text = response.Substring(0, 200); }));
        }
            private void textBox3_TextChanged(object sender, EventArgs e)
        {

        }

        private void textBox2_TextChanged(object sender, EventArgs e)
        {

        }
    }
}
